package com.supply.dl.utils.utils;

import android.content.Context;
import com.supply.dl.constant.Constants;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class Logger
{
	private static final String TAG = "sdl logger";

	private static java.util.logging.Logger logger = null;

	public static void i(String message)
	{
		logHandler(TAG, message, Level.INFO);
	}

	public static void init(Context context)
	{
		if (logger != null)
			return;

		final Formatter formatter = new Formatter()
		{
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);

			@Override public String format(LogRecord r)
			{
				String msg = r.getMessage();
				return dateFormat.format(new Date(r.getMillis())) + "-" + r.getLevel() + ":" + msg + "\n";
			}
		};
		java.util.logging.Logger log = java.util.logging.Logger.getLogger(TAG);

		try
		{
			File logFile = genLogFile(context);

			boolean nf = logFile.isDirectory();

			/**控制不输出到默认日志：*/
			log.setUseParentHandlers(nf);
			log.setLevel(Level.ALL);

			/*文件输出*/
			if (!nf && logFile.getParentFile().exists())
			{
				FileHandler handler = new FileHandler(logFile.getAbsolutePath(), true);
				handler.setLevel(Level.ALL);
				handler.setFormatter(formatter);
				log.addHandler(handler);
			}

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		logger = log;
		initDefaultExceptionHandler();
	}

	public static void i(String tag, String message)
	{
		logHandler(tag, message, Level.INFO);
	}

	public static void e(String message)
	{
		logHandler(TAG, message, Level.WARNING);
	}

	public static void e(String tag, String message)
	{
		logHandler(tag, message, Level.WARNING);
	}

	public static void w(String message)
	{
		logHandler(TAG, message, Level.INFO);
	}

	public static void w(String tag, String message)
	{
		logHandler(tag, message, Level.INFO);
	}

	public static void d(String message)
	{
		logHandler(TAG, message, Level.INFO);
	}

	public static void d(String tag, String message)
	{
		logHandler(tag, message, Level.INFO);
	}

	public static void v(String message)
	{
		logHandler(TAG, message, Level.INFO);
	}

	public static void v(String tag, String message)
	{
		logHandler(tag, message, Level.INFO);
	}

	private static void Thread(String tag, Throwable error)
	{
		StackTraceElement[] stackTrace = error.getStackTrace();
		StringBuffer sb = new StringBuffer();
		sb.append(error.getMessage());
		for (int i = 0; i < stackTrace.length; i++)
		{
			sb.append("file:").append(stackTrace[i].getFileName()).append(" class:").append(stackTrace[i].getClassName()).append(" method:").append(stackTrace[i].getMethodName()).append(" line:").append(stackTrace[i].getLineNumber()).append("\n");
		}
		logHandler(TAG, sb.toString(), Level.WARNING);
	}

	private static void logHandler(String tag, String message, Level level)
	{
		if (logger != null)
		{
			logger.log(level, tag + " " + message);
			android.util.Log.e(tag, message + "");
		}
	}

	private static File genLogFile(Context context)
	{
		File path_zzsdk = new File(Constants.Path.PATH_DATA);
		File path_log = new File(path_zzsdk, "log");
		File path_dir = new File(path_log, context.getPackageName());
		String logFileName = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(new Date());
		return new File(path_dir, logFileName + ".log");
	}

	public static void initDefaultExceptionHandler()
	{
		final Thread.UncaughtExceptionHandler defaultHanlder = Thread.getDefaultUncaughtExceptionHandler();
		Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
		{
			Thread.UncaughtExceptionHandler mDefaultHandler = defaultHanlder;

			@Override public void uncaughtException(Thread thread, Throwable ex)
			{
				Logger.Thread(String.valueOf(thread), ex);
				if (logger != null)
				{
					logger.log(Level.SEVERE, String.valueOf(thread), ex);
				}
				if (mDefaultHandler != null)
				{
					mDefaultHandler.uncaughtException(thread, ex);
				}
			}
		});
	}
}

